#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
'''
Created on 2016年6月2日
@author: zhaoxiang
根据 论坛，问答，行程，目的地，锦囊，折扣，，点评，酒店等业务表
解析出用户相关行为内容(id，city, country,..)
'''
import sys,os
sys.path.append('/home/hadoop/udf_data/python/user_track')
os.environ['PYTHON_EGG_CACHE'] = '/tmp/hadoop/.python-eggs/'
os.environ['PYTHON_EGG_DIR']='/tmp/hadoop/.python-eggs/'
import datetime,format_time
from DB import DB
import gua,config
import ConfigParser
from util_func import dateToTimestamp
from user_action_z import collection_sep,  k_v_pattern
reload(sys)
sys.setdefaultencoding("utf8")
conf = ConfigParser.ConfigParser() 
conf.read("config_sql.cfg")
    
outputPattern="\t".join(["%s"]*11) +"\n"
#==============================
#折扣
#rowSet=[user_id, unique_id, ugc_type, ugc_content,platform, ip, lon, lat, client_id, detail,channel]
#===============================
def buy_lm(starttime, endtime) :
    qyer_db = DB(gua.conn_qyer["host"], gua.conn_qyer["username"], gua.conn_qyer["password"], gua.conn_qyer["db"], gua.conn_qyer["port"])
    #订单信息
    qyer_db.execute(conf.get("z", "z_order"), (starttime, endtime))
    result_set = qyer_db.fetchall()
    
    channel='z'
    parsed_type='z_book'
    strDate = format_time.timeStamp2DateStr(starttime)
    fileName=config.user_action_product_outputDir % strDate
    if not os.path.exists(os.path.dirname(fileName)):
        os.mkdir(os.path.dirname(fileName));
    with open(fileName,"w") as f:
        for result in result_set :
            tmp = [k_v_pattern % (k,v if v is not None else "") for (k,v) in result.iteritems() ]
            uid = result["uid"]
            lid = result["lid"]
            #查折扣本身的其他相关信息
            #"select product_type,title,pay_time,start_time,start_pos,country,countryids,city,cityids from qyer_lastminute.lastminute_main where id = %s"
            qyer_db.execute(conf.get("z", "z_detail"), (lid,))
            lm_res = qyer_db.fetchone()
            if lm_res :
                tmp += [ k_v_pattern % (k,v if v is not None else "") for (k,v) in lm_res.iteritems()]
            detail_info = collection_sep.join(tmp).replace("\n", "").replace("\r", "")
            rowSet=(uid,"-", parsed_type, lid,"","","","","",detail_info,channel)
            f.write(outputPattern % rowSet)
    qyer_db.close() 

#锦囊下载，收藏，评论
def jn_down(starttime, endtime) :
    strDate = format_time.timeStamp2DateStr(starttime)
    fileName=config.user_action_product_outputDir % strDate
    if not os.path.exists(os.path.dirname(fileName)):
        os.mkdir(os.path.dirname(fileName));
        
    qyer_db = DB(gua.conn_qyer["host"], gua.conn_qyer["username"], gua.conn_qyer["password"], gua.conn_qyer["db"], gua.conn_qyer["port"])
    #下载信息
    qyer_db.execute(conf.get("jn", "jn_down"), (starttime, endtime))
    result_set = qyer_db.fetchall()
    channel='guide'
    parsed_type='jn_down'
    with open(fileName,"a") as f:
        for result in result_set :
            tmp = []
            uid = result["uid"]
            x_id = result["jid"]
            #查折扣本身的其他相关信息
            #"select product_type,title,pay_time,start_time,start_pos,country,countryids,city,cityids from qyer_lastminute.lastminute_main where id = %s"
            qyer_db.execute(conf.get("jn", "jn_detail"), (x_id,))
            lm_res = qyer_db.fetchone()
            if lm_res :
                tmp = [ k_v_pattern % (k,v if v is not None else "") for (k,v) in lm_res.iteritems()]
            detail_info = collection_sep.join(tmp).replace("\n", "").replace("\r", "")
            rowSet=(uid,"-", parsed_type, x_id,"","","","","",detail_info,channel)
            f.write(outputPattern % rowSet)
            
    #评论信息
    qyer_db.execute(conf.get("jn", "jn_comment"), (starttime, endtime))
    result_set = qyer_db.fetchall()
    parsed_type='jn_comment'
    with open(fileName,"a") as f:
        for result in result_set :
            tmp = []
            uid = result["uid"]
            x_id = result["jid"]
            #本身的其他相关信息
            #"select product_type,title,pay_time,start_time,start_pos,country,countryids,city,cityids from qyer_lastminute.lastminute_main where id = %s"
            qyer_db.execute(conf.get("jn", "jn_detail"), (x_id,))
            lm_res = qyer_db.fetchone()
            if lm_res :
                tmp = [ k_v_pattern % (k,v if v is not None else "") for (k,v) in lm_res.iteritems()]
            detail_info = collection_sep.join(tmp).replace("\n", "").replace("\r", "")
            rowSet=(uid,"-", parsed_type, x_id,"","","","","",detail_info,channel)
            f.write(outputPattern % rowSet)

    #收藏信息
    qyer_db.execute(conf.get("jn", "jn_favorite"), (starttime, endtime))
    result_set = qyer_db.fetchall()
    parsed_type='jn_favorite'
    with open(fileName,"a") as f:
        for result in result_set :
            tmp = []
            uid = result["uid"]
            x_id = result["jid"]
            #查锦囊本身的其他相关信息
            #"select product_type,title,pay_time,start_time,start_pos,country,countryids,city,cityids from qyer_lastminute.lastminute_main where id = %s"
            qyer_db.execute(conf.get("jn", "jn_detail"), (x_id,))
            lm_res = qyer_db.fetchone()
            if lm_res :
                tmp = [ k_v_pattern % (k,v if v is not None else "") for (k,v) in lm_res.iteritems()]
            detail_info = collection_sep.join(tmp).replace("\n", "").replace("\r", "")
            rowSet=(uid,"-", parsed_type, x_id,"","","","","",detail_info,channel)
            f.write(outputPattern % rowSet)
            
            
    qyer_db.close() 

#锦囊收藏

#锦囊评论


def dump_user_action(theday) :
    starttime = dateToTimestamp(theday)

    endtime = starttime + 60*60*24
    print format_time.get_now(), "======begin deal with %s=====" % theday
    print format_time.get_now(), "buy lm"
    buy_lm(starttime, endtime)
    print format_time.get_now(), "jn_down"
    jn_down(starttime, endtime)
    print format_time.get_now(), "======end deal with %s=====" % theday

def main(*argv) :
    #默认跑昨天的数据
    if len(argv) == 1 :
        startDay = datetime.datetime.now() - datetime.timedelta(days=1)
        startDay = datetime.datetime(startDay.year, startDay.month, startDay.day)
        dump_user_action(startDay)
    #指定某一天
    elif len(argv) == 2 : 
        startDay = datetime.datetime.strptime(argv[1], "%Y%m%d")
        dump_user_action(startDay)
    #指定起止日期
    elif len(argv) == 3 :
        startDay = datetime.datetime.strptime(argv[1], "%Y%m%d")
        endDay = datetime.datetime.strptime(argv[2], "%Y%m%d")
        while startDay < endDay :
            dump_user_action(startDay)
            startDay += datetime.timedelta(days = 1)


if __name__ == '__main__' :
    main(*sys.argv)
